<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Specs_model extends MY_Model {
	var $table = 'specification';
	
	// 构造函数
	public function __construct() {
		// 调用父类构造函数，必不可少
		parent::__construct ();
	}
	
	/**
	 * 获取所有规格或者根据product_id查找
	 *
	 * @param integer $id        	
	 * @return array
	 */
	public function getAll($id, $config = array(), $like = array()) {
		$where = array ();
		if (! empty ( $id )) {
			$where ['product_id'] = $id;
		}
		$config ['totalCount'] = $this->get_search_num ( $where, $like );
		if ($config ['offset'] >= $config ['totalCount']) {
			$config ['offset'] = 0;
			$config ['pageNum'] = 1;
		}
		$config ['data'] = $this->search ( $where, $config, $like );
		return $config;
	}
	
	/**
	 * 增加规格
	 *
	 * @param array $data        	
	 * @return integer
	 */
	public function addSpecs($id, $data) {
		// 插入规格
		$this->db->trans_start();
		foreach ( $data as &$v ) {
			$v ['product_id'] = $id;
			$v ['created_at'] = time ();
		}
		$this->db->insert_batch ( $this->table, $data );
		$this->db->trans_complete();
		
		return $this->db->trans_status();
	}
	
	/**
	 * 删除规格
	 *
	 * @param string $ids
	 *        	多个用逗号隔开
	 * @return integer
	 */
	public function deleteSpecs($ids) {
		$idsArr = explode ( ',', $ids );
		return $this->delete ( array (
				'id' => $idsArr 
		) );
	}
	
	/**
	 * 更新规格信息
	 *
	 * @param array $data        	
	 * @param integer $id        	
	 * @return integer
	 */
	public function updateSpecs($data, $id) {
		return $this->update ( $data, array (
				'id' => $id 
		) );
	}
	
	/**
	 * 查找详细信息
	 *
	 * @param integer $id
	 * @return array
	 */
	public function findSpecs($id) {
		return $this->get_row(array('id'=>$id));
	}
}